** This do-file extracts parliamentary election results from Manifesto Project

use "$project_path/data/1_input/elections/MP/MPDataset_MPDS2018a_stata14.dta", clear

keep countryname partyname partyabbrev pervote absseat totseats edate 
ren countryname Country

// Incorprate party abbreviation in party name
replace partyname = partyname`i' + " ("+partyabbrev+")" if partyabbrev!=""
drop partyabbrev
ren partyname Party_
ren absseat Seats_
format Party_ %25s

// Seat shares
ren totseats Total_Seats
gen double Seat_Share_ = Seats_/Total_Seats

// Reshape and rank parties by their number of seats
gsort Country edate -Seats_ -pervote Party_
drop pervote
by Country edate: gen id = _n
reshape wide Party_ Seats_ Seat_Share_, i(Country edate) j(id)

// Month and year variables
gen Type_Election = "Parliamentary"
gen Year = year(edate)
gen Month = month(edate)
bysort Country Year: gen temp = _N
replace Month = . if temp==1
drop temp edate
order Country Year Month Type_Election Total_Seats

// Only keeping elections between 1946 and 2018
keep if Year>1945 & Year<=2018

// Adding missing variables
forvalues k = 22/75 {
	gen Party_`k' = ""
	gen Seats_`k' = .
	gen Seat_Share_`k' = .
}
foreach v in flag_wrong_sum_seats flag_inconsequential flag_vacant_seats flag_vacant_seats_nb flag_appointed flag_appointed_nb flag_non_partisan flag_coup {
	gen `v' = .
}
gen flag_inconsequential_note = ""
order flag_inconsequential_note, after(flag_inconsequential)

// Adjusting country names
replace Country="Bosnia-Herz"  if Country=="Bosnia-Herzegovina"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="GDR"  if Country=="German Democratic Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Korea, Republic of"  if Country=="South Korea"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States"

// Manual adjustments
replace Month = 11 if Country=="France" & Year==1946
replace Month = 7 if Country=="Moldova" & Year==2009
replace Month = 7 if Country=="Sri Lanka" & Year==1960
replace Month = 4 if Country=="Iceland" & Year==1991
replace Month = 12 if Country=="Serbia" & Year==1992

// Wrong sum of seats
egen sum_seats_parties = rowtotal(Seats_*)
replace flag_wrong_sum_seats = 1 if sum_seats_parties!=Total_Seats
drop sum_seats_parties

lab var Country  "Country in which election took place"
lab var Year     "Year of the election"
lab var Month    "Month of the election"
lab var Type_Election    "Election type"
lab var flag_wrong_sum_seats		"Sum of elected seats in parliament is inconsistent with the total number of seats"
lab var flag_inconsequential		"Flags cancelled or inconsequential elections"
lab var flag_inconsequential_note		"Why flag_inconsequential is equal to 1"
lab var flag_coup		"Flags elections that were shortly followed by a coup or revolution"
lab var flag_vacant_seats		"Flags elections which left vacant seats in parliament"
lab var flag_vacant_seats_nb		"Number of vacant seats in parliament"
lab var flag_appointed		"Flags elections in which some seats were not elected directly"
lab var flag_appointed_nb		"Number of seats for which MPs were not elected directly"
lab var flag_non_partisan		"Flags elections with no parties"
lab var Total_Seats			"Total number of seats"
forvalues i=1/75 {
	lab var Party_`i'        "Party or coalition n°`i'"
	lab var Seats_`i'  "Seats won by party n°`i'"
	lab var Seat_Share_`i'  "Seat share of party n°`i'"
	replace Seat_Share_`i' = 100*Seat_Share_`i'
}

compress
format Country %30s
format Type_Election %15s
format Party_* %25s
recast double Seat_Share_*
notes drop _dta

save "$project_path/data/2_intermediary/elections/Parliamentary elections/parl_elec_mp", replace
